gen-demo-header.py: Fix running without git
authorChun-wei Fan <fanchunwei@src.gnome.org>
Tue, 18 May 2021 03:02:27 +0000 (11:02 +0800)
committerChun-wei Fan <fanchunwei@src.gnome.org>
Tue, 18 May 2021 03:25:20 +0000 (11:25 +0800)
The script assumes that the git program is in the PATH which may not be always
the case, so if git was not found, fallback to the former behavior of making
VCS_TAG an empty string if the profile option was not set to 'devel'.

Also fix the call to the script so that we do indeed call GIT on the source GIT
repo so that we can get the right short SHA1 sum.

Re-organize the code a bit in the process, so that things seem cleaner.

build-aux/meson/gen-demo-header.py
demos/meson.build

index 700193a50b56a082d84bd95dd62862c93b447893..b84a6a9ac0b14511894b21aa2140d2bad9edeba4 100755 (executable)
@@ -4,7 +4,8 @@ import os
 import subprocess
 import sys
 
-profile = sys.argv[1]
+repodir = sys.argv[1]
+profile = sys.argv[2]
 
 sys.stdout.write("/* This file is auto-generated. Do not edit. */\n")
 sys.stdout.write("#pragma once\n")
@@ -12,10 +13,14 @@ sys.stdout.write("\n")
 sys.stdout.write(f"#define PROFILE \"{profile}\"\n")
 
 short_sha = os.environ.get('CI_COMMIT_SHORT_SHA')
-if short_sha is not None:
-    sys.stdout.write(f"#define VCS_TAG \"{short_sha}\"\n")
-else:
-    cmd = ["git", "rev-parse", "--short", "HEAD"]
-    with subprocess.Popen(cmd, stdout=subprocess.PIPE) as p:
-        short_sha = p.stdout.read().decode('utf-8').rstrip("\n")
-        sys.stdout.write(f"#define VCS_TAG \"{short_sha}\"\n")
+if short_sha is None:
+    cmd = ["git", "-C", repodir, "rev-parse", "--short", "HEAD"]
+    try:
+        with subprocess.Popen(cmd, stdout=subprocess.PIPE) as p:
+            short_sha = p.stdout.read().decode('utf-8').rstrip("\n")
+    except FileNotFoundError:
+        short_sha = ''
+        if profile != 'default':
+            short_sha = 'devel'
+
+sys.stdout.write(f"#define VCS_TAG \"{short_sha}\"\n")
index c66d19308bf9feb66e157e29a5d97edde26d39b3..5b68828eef7f6cd419e678e7c54c9baff3a2641e 100644 (file)
@@ -3,7 +3,7 @@ demo_profile = get_option('profile')
 
 demo_conf_h = declare_dependency(
   sources: custom_target('demo-header',
-    command: [gen_demo_header, demo_profile],
+    command: [gen_demo_header, meson.source_root(), demo_profile],
     capture: true,
     output: 'demo_conf.h',
     build_by_default: true,